$(function () {
    $("#jqGrid").jqGrid({
        url: '/role/list',
        datatype: "json",

        colModel: [
            { label: '角色ID', name: 'id', index: "id", width: 45, key: true },
            { label: '角色名称', name: 'name', index: "name", width: 75 },
            { label: '英文名称', name: 'enname', index: "enname", width: 75 },
            { label: '角色类型', name: 'roleType', index: "roleType", width: 75 },
            { label: '创建时间', name: 'createTime', index: "createTime", width: 80},
            { label: '修改时间', name: 'updateTime', index: "updateTime", width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "result.records",
            page: "result.current",
            total: "result.pages",
            records: "result.total"
        },
        prmNames : {
            page:"current",
            rows:"size"
            // order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

//菜单树
var menu_ztree;
var menu_setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: 0
        },
        key: {
            url:"nourl"
        }
    },
    check:{
        enable:true,
        nocheckInherit:true
    }
};


var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            name: null
        },
        showList: true,
        title:null,
        role:{

        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.role = {};
            vm.getMenuTree(null);
        },
        update: function () {
            var roleId = getSelectedRow();
            if(roleId == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";
            vm.getMenuTree(roleId);
            vm.getRole(roleId);
        },
        del: function () {
            var roleIds = getSelectedRows();
            debugger
            if(roleIds == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                var dataJson =  {"roleIds": roleIds} ;
                $.ajax({
                    type: "POST",
                    url: "/role/delete",
                    contentType: "application/json",
                    data: JSON.stringify(dataJson),
                    dataType: "json",
                    success: function(r){
                        if(r.status.statusCode == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.status.statusReason);
                        }
                    },
                    error:function(r){
                        confirm('由于长时间未操作，确定要重新登陆吗？', function(){
                            window.location.href = "/views/login.html" ;
                        });
                    }
                });
            });
        },
        getRole: function(roleId){
            $.ajax({
                type: "GET",
                url: "/role/info?id="+roleId,
                dataType: "json",
                success: function(r){
                    if(r.status.statusCode == 0){
                        vm.role = r.result;

                    }else{
                        alert(r.status.statusReason);
                    }
                },
                error:function(r){
                    confirm('由于长时间未操作，确定要重新登陆吗？', function(){
                        window.location.href = "/views/login.html" ;
                    });
                }
            });

        },
        saveOrUpdate: function () {
            //获取选择的菜单
            var nodes = menu_ztree.getCheckedNodes(true);
            var menuIdList = new Array();
            for(var i=0; i<nodes.length; i++) {
                menuIdList.push(nodes[i].id);
            }
            vm.role.menuIdList = menuIdList;
console.log(JSON.stringify(vm.role));
            $.ajax({
                type: "POST",
                url: "/role/saveOrUpdate",
                contentType: "application/json",
                data: JSON.stringify(vm.role),
                success: function(r){
                    if(r.status.statusCode === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.status.statusReason);
                    }
                },
                error:function(r){
                    confirm('由于长时间未操作，确定要重新登陆吗？', function(){
                        window.location.href = "/views/login.html" ;
                    });
                }
            });
        },
        getMenuTree: function(roleId) {
            var roleIdStr = "" ;
            if(roleId != null) {
                roleIdStr = roleId+"";
            }
            //加载菜单树
            $.ajax({
                type: "GET",
                url: "/menu/getMenuTreeListByRoleId?roleId="+roleIdStr,
                dataType: "json",
                success: function(r){
                    if(r.status.statusCode === 0){
                        menu_ztree = $.fn.zTree.init($("#menuTree"), menu_setting, r.result);
                        //展开所有节点
                        menu_ztree.expandAll(true);
                    }else{
                        alert(r.status.statusReason);
                    }
                }
            });

        },

        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'name': vm.q.name},
                page:page
            }).trigger("reloadGrid");
        }
    }
});